import pygame
import sys

#GLOBAL VARIABLES
width, height = 800, 600

BLACK = (0,0,0)
WHITE = (255,255,255)
GREY = (88,88,88)
GREY_DARKER = (100,100,100)

#BOAT
boat = pygame.image.load("./images/Boat.png")
boat.set_colorkey(WHITE)
boat = pygame.transform.scale(boat, (40,40))
boat_pos = (250,195)
boat_rect = pygame.Rect(boat_pos[0], boat_pos[1], boat.get_width(), boat.get_height())

#DUCK
duck = pygame.image.load("./images/Duck.png")
duck.set_colorkey(WHITE)
duck_pos = (270, 250)
start_duck_pos = duck_pos

duck_rect = pygame.Rect(start_duck_pos[0], start_duck_pos[1], duck.get_width(), duck.get_height())

#PILLOW
pillow = pygame.image.load("./images/Pillow.png")
pillow.set_colorkey(WHITE)
corn_pos = (220, 250)
start_corn_pos = corn_pos

pillow_rect = pygame.Rect(start_corn_pos[0], start_corn_pos[1], pillow.get_width(), duck.get_height())

#FOX 
fox = pygame.image.load("./images/Fox.png")
fox.set_colorkey(WHITE)
fox_pos = (180, 250)
start_fox_pos = fox_pos

fox_rect = pygame.Rect(start_fox_pos[0], start_fox_pos[1], fox.get_width(), fox.get_height())

background_river = pygame.image.load("./images/Background1.png")

class Rectangle:
	def __init__(self, x, y, width, height):
		self.x = x
		self.y = y
		self.width = width
		self.height = height

	def draw(self, displ):
		rect = pygame.Rect(self.x, self.y, self.width, self.height)
		pygame.draw.rect(displ, GREY, rect)

def initialize():
	pygame.init()
	window = pygame.display.set_mode((width, height))
	return window

def if_in_boat(in_boat, boat_rect):
	for is_in_boat, rect in in_boat.items():
		if is_in_boat == True:
			rect.x , rect.y = boat_rect.x , boat_rect.y

def update_to_start_pos(start_pos1, start_pos2, start_pos3):
	duck_rect.x, duck_rect.y = start_pos1[0], start_pos1[1]
	pillow_rect.x, pillow_rect.y = start_pos2[0], start_pos2[1]
	fox_rect.x, fox_rect.y = start_pos3[0], start_pos3[1]

def main(window):
	disp = pygame.Surface((width//2, height//2))

	rect1 = pygame.Rect(0,0,width//2, 50)
	rect2 = pygame.Rect(0,height//2-50,width//2,50)

	clock = pygame.time.Clock()

	drag_drop_fox = False
	drag_drop_duck = False
	drag_drop_pillow = False
	
	fox_in_boat = False
	corn_in_boat = False
	duck_in_boat = False

	in_boat = {}

	offset_x = 0
	offset_y = 0

	#COAST UP , COAST BOTTOM
	coast_up_line = [[0, 100], [width//2, 100], 5]
	coast_bottom_line = [[0, 200], [width//2, 200], 5]


	while True:
		in_boat = {fox_in_boat : fox_rect, corn_in_boat : pillow_rect, duck_in_boat : duck_rect}

		#WHITE RIVER (ALL BACKGROUND)
		disp.fill(WHITE)

		#LINES 
		pygame.draw.line(disp, BLACK, (0, 100), (width//2, 100), 5)
		pygame.draw.line(disp, BLACK, (0, 200), (width//2, 200), 5)

		#UP == 0
		if boat_rect.y < 100:
			duck_pos = (100, 100)
			corn_pos = (200, 200)
			fox_pos = (250, 250)
		#DOWN == 1
		if boat_rect.y > 100:
			duck_pos = start_duck_pos
			corn_pos = start_corn_pos
			fox_pos = start_fox_pos

		#BOAT
		disp.blit(boat, (boat_rect.x, boat_rect.y))

		#COASTS
		pygame.draw.rect(disp, (GREY_DARKER), rect1)
		pygame.draw.rect(disp, (GREY_DARKER), rect2)

		#DUCK
		disp.blit(duck, (duck_rect.x, duck_rect.y))	

		#PILLOW
		disp.blit(pillow, (pillow_rect.x, pillow_rect.y))

		#FOX
		disp.blit(fox, (fox_rect.x, fox_rect.y))

		for event in pygame.event.get():
			if event.type == pygame.QUIT:
				pygame.quit()
				sys.exit()

			if event.type == pygame.KEYDOWN:

				if event.key == pygame.K_DOWN:
					if_in_boat(in_boat, boat_rect)

					boat_rect.y += 10
					if boat_rect.y <= 50:
						boat_rect.y = 50
					#50
					#---
					#210
					if boat_rect.y >= 210:
						boat_rect.y = 210

				if event.key == pygame.K_UP:
					if_in_boat(in_boat, boat_rect)
					
					boat_rect.y -= 10
					if boat_rect.y <= 50:
						boat_rect.y = 50
					#50
					#---
					#210
					if boat_rect.y >= 210:
						boat_rect.y = 210

			elif event.type == pygame.MOUSEBUTTONDOWN:
				if event.button == 1:

					if boat_rect.collidepoint((event.pos[0]//2, event.pos[1]//2)):
						update_to_start_pos(duck_pos, corn_pos, fox_pos)


					#DUCKFOXCORN COLLIDE MOUSE

					#FOX COLLIDE MOUSE
					if fox_rect.collidepoint((event.pos[0]//2, event.pos[1]//2)):
						drag_drop_fox = True
						drag_drop_pillow = False
						drag_drop_duck = False
						m_x, m_y = event.pos[0]//2, event.pos[1]//2
						offset_x = fox_rect.x - m_x
						offset_y = fox_rect.y - m_y					

					#CORN COLLIDE MOUSE
					if pillow_rect.collidepoint((event.pos[0]//2, event.pos[1]//2)):
						drag_drop_fox = False
						drag_drop_pillow = True
						drag_drop_duck = False
						m_x, m_y = event.pos[0]//2, event.pos[1]//2
						offset_x = pillow_rect.x - m_x
						offset_y = pillow_rect.y - m_y	

					#DUCK COLLIDE MOUSE
					if duck_rect.collidepoint((event.pos[0]//2, event.pos[1]//2)):
						drag_drop_fox = False
						drag_drop_pillow = False
						drag_drop_duck = True
						m_x, m_y = event.pos[0]//2, event.pos[1]//2
						offset_x = duck_rect.x - m_x
						offset_y = duck_rect.y - m_y	

			elif event.type == pygame.MOUSEBUTTONUP:
				if event.button == 1:
					drag_drop_fox = False
					drag_drop_pillow = False
					drag_drop_duck = False

			#IF MOUSE CLICKED AND HOLD
			elif event.type == pygame.MOUSEMOTION:

				# DRAG AND DROP FOX
				if drag_drop_fox:
					m_x, m_y = event.pos[0]//2, event.pos[1]//2
					fox_rect.x = m_x + offset_x
					fox_rect.y = m_y + offset_y
					if fox_rect.colliderect(boat_rect):
						fox_in_boat = True
						fox_rect.x, fox_rect.y = boat_rect.x, boat_rect.y

				# DRAG AND DROP CORN
				if drag_drop_pillow:
					m_x, m_y = event.pos[0]//2, event.pos[1]//2
					pillow_rect.x = m_x + offset_x
					pillow_rect.y = m_y + offset_y
					if pillow_rect.colliderect(boat_rect):
						corn_in_boat = True
						pillow_rect.x, pillow_rect.y = boat_rect.x, boat_rect.y

				# DRAG AND DROP DUCK
				if drag_drop_duck:
					m_x, m_y = event.pos[0]//2, event.pos[1]//2
					duck_rect.x = m_x + offset_x
					duck_rect.y = m_y + offset_y
					if duck_rect.colliderect(boat_rect):
						duck_in_boat = True
						duck_rect.x, duck_rect.y = boat_rect.x, boat_rect.y

		surface = pygame.transform.scale(disp, (width,height))
		window.blit(surface, (0,0))

		pygame.display.update()

		clock.tick(30)



if __name__ == '__main__':
	main(initialize())